################################################################################ 
#
# Replication code for:
# Empathy-based counterspeech can reduce racist hate speech
# in a social media field experiment
#
# PNAS 2021
# 
# SRU model
#
################################################################################ 



rm(list = ls())
setsave = T

################################################################################ 
#  LIBRARIES
################################################################################ 

library(dplyr)
library(tidyr)
library(xtable)
library(foreign)
library(systemfit)
library(stringr)
library(readr) 

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = dirname(dirname(dirname(rstudioapi::getActiveDocumentContext()$path)))
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')


# Upload the data
setwd(wd_data)
data = read.csv('main_dataset.csv')
data = data[(is.na(data$deleted_suspended)), ]

# Upload the controls
lassodata = read_rds('data_lasso2.RDS')
lassodata = lassodata[-1]
data = cbind(data, lassodata)

# Upload main results
container = read.csv('auxiliary/container_lasso_controls.csv')


################################################################################ 
#   VARIABLES
################################################################################

data$empathy_dummy = ifelse(data$treatment_main==3, 1, 0)
data$consequences_dummy = ifelse(data$treatment_main==2, 1, 0)
data$meme_dummy = ifelse(data$treatment_main==1, 1, 0)

data$treated = ifelse(data$ITT==0 & data$treatment_dummy==1, 1, 0)

data$pre_hate_share_tot = data$num_hate_tweets_pre / data$num_tweets_pre

################################################################################ 
#   Prepare
################################################################################ 


outvars = c('num_hate_tweets_post', 'num_tweets_post', 'post_hate_share_tot',
            'outcome_deleted_specific', 'deletion_rate', 'tox_vader_neu_post')

categories = c('Hate Speech\nCreation', 'Hate Speech\nCreation', 'Hate Speech\nCreation',
               'Hate Speech\nDeletion', 'Hate Speech\nDeletion', 'Tone')

treatments = c('meme_dummy', 'consequences_dummy', 'empathy_dummy')
treatments_lab = c( 'Meme', 'Warning of Consequences',  'Empathy')

outvars_name = c( 'Hate Tweets Count', 'Tweets Count', 'Hate Tweets Rate',
                  'Hate Tweet Not Deleted', 'Non-Deletion Rate',
                  'Vader Negativity')


################################################################################ 
#  SRU models 
################################################################################ 

## Humor 

temp = data[data$treatment_main %in% c(0,1),]

models = list()
models[['hate']] = num_hate_tweets_post ~ treatment_dummy
models[['tweets']] = num_tweets_post ~ treatment_dummy
models[['hateshare']] = post_hate_share_tot ~ treatment_dummy
models[['deleted']] = outcome_deleted_specific ~ treatment_dummy

fitsur1 <- systemfit(models, method = "SUR", data=temp)
summary(fitsur1, residCov = FALSE, equations = FALSE)



## Warning of Consequences

temp = data[data$treatment_main %in% c(0,2),]

models = list()
models[['hate']] = num_hate_tweets_post ~ treatment_dummy
models[['tweets']] = num_tweets_post ~ treatment_dummy
models[['hateshare']] = post_hate_share_tot ~ treatment_dummy
models[['deleted']] = outcome_deleted_specific ~ treatment_dummy

fitsur2 <- systemfit(models, method = "SUR", data=temp)
summary(fitsur2, residCov = FALSE, equations = FALSE)


### Empathy 

temp = data[data$treatment_main %in% c(0,3),]

models = list()
models[['hate']] = num_hate_tweets_post ~ treatment_dummy
models[['tweets']] = num_tweets_post ~ treatment_dummy
models[['hateshare']] = post_hate_share_tot ~ treatment_dummy
models[['deleted']] = outcome_deleted_specific ~ treatment_dummy

fitsur3 <- systemfit(models, data=temp, method = "SUR")
summary(fitsur3, residCov = FALSE, equations = FALSE)


# Testing joint insignificance

restriction = paste0(c('hate', 'tweets', 'hateshare', 'deleted'), '_treatment_dummy =0')

linearHypothesis(fitsur1, restriction, white.adjust = "hc1")
linearHypothesis(fitsur2, restriction, white.adjust = "hc1")
linearHypothesis(fitsur3, restriction, white.adjust = "hc1")

